package com.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author south wind
 * @version 1.0
 * @date 2025/4/2 19:27
 * @description  创建一个list类，在list类中增加三个工人
 */
public class Test01 {
    public static void main(String[] args) {
        //1.创建list类
        List<Worker> workerList = new ArrayList<>();
        workerList.add(new Worker("zhang3",18,3000));
        workerList.add(new Worker("li4",25,3500));
        workerList.add(new Worker("wang5",22,3200));

        //2.在li4前插入一个工人
        int index=0;
        for (Worker worker : workerList){
            if ("li4".equals(worker.getName())){
                break;
            }
            index++;
        }
        workerList.add(index,new Worker("zhao6",24,3300));
        //3.删除王五的信息
        for (Iterator<Worker> iterator = workerList.iterator(); iterator.hasNext();){
            Worker worker = iterator.next();
            if ("wang5".equals(worker.getName())){
                iterator.remove();
            }

        }
        //4.利用for循环遍历，打印List中所有工人的信息
        System.out.println("所有工人的信息如下：");
        for (Worker worker : workerList){
            System.out.println(worker);
        }
        //5.迭代遍历，对list所有工人调用work方法
        System.out.println("所有工人开始工作：");
        for (Iterator<Worker> s = workerList.listIterator();s.hasNext();){
            Worker worker = s.next();
            worker.work();
        }
    }
}
